package com.goldcn.sys.controller;

import com.goldcn.sys.dto.MenuDto;
import com.goldcn.sys.dto.MenuMetaDto;
import com.goldcn.sys.model.SysMenuItem;
import com.goldcn.sys.service.SysMenuService;
import com.goldcn.sys.service.SysTableService;
import com.goldcn.template.utils.ExmallEntity;
import com.goldcn.template.utils.Field;
import com.goldcn.template.utils.GeneratorCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author Tim Peng
 * mail: lmn_022@163.com
 * Date: 2017/6/5
 * Time: 16:54
 * To change this template use File | Settings | File Templates.
 */
@RestController
@RequestMapping(value = "/system")
@Api(value = "system", description = "实体(表结构)管理")
public class SysTableController {

    @Autowired
    SysTableService sysTableService;

    @Autowired
    SysMenuService sysMenuService;

    @ApiOperation(value = "查询table")
    @RequestMapping(value = "/table/listPage", method = RequestMethod.GET)
    public Object selectTablePageCondition(@RequestParam(value = "pageNo", required = false) Integer pageNo, @RequestParam(value = "pageSize", required = false) Integer pageSize) {

        return sysTableService.selectTablePageCondition(pageNo, pageSize);
    }

    @ApiOperation(value = "code gen")
    @RequestMapping(value = "/code/make/{pageNum}/{pageSize}", method = RequestMethod.GET)
    public Object makeCode(@PathVariable Integer pageNum, @PathVariable Integer pageSize) {
        Map<String, Object> map = new HashMap<String, Object>();

        map.put("className", "SysUser");
        map.put("tableName", "sys_user");
        map.put("package", "com.goldcn.demo");

        ExmallEntity exmallEntity = new ExmallEntity();
        List<Field> fieldList = new ArrayList<>();
        Field field = new Field();
        field.setFieldName("userName");
        field.setFieldType("String");
        field.setFieldRemarks("用户名称");
        field.setFieldNameUpperFirstLetter("userName");
        field.setQueryFlag(false);

        Field field1 = new Field();
        field1.setFieldName("userName1");
        field1.setFieldType("String");
        field1.setFieldRemarks("用户名称1");
        field1.setFieldNameUpperFirstLetter("userName1");
        field1.setQueryFlag(false);

        fieldList.add(field);
        fieldList.add(field1);

        exmallEntity.setFieldList(fieldList);

        map.put("className", "SysUser");
        map.put("tableName", "sys_user");
        map.put("package", "com.goldcn.demo");
        map.put("exmallEntity", exmallEntity);


        GeneratorCode.generator(map, "SysUser", "model.ftl", "e:\\wancai\\src");


        //return sysTableService.selectTablePageCondition(pageNum, pageSize);
        return "ok";
    }


    @ApiOperation(value = "查询table")
    @RequestMapping(value = "/column/listPage", method = RequestMethod.GET)
    public Object selectColumnPageCondition(@RequestParam(value = "mainId", required = false) String mainId, @RequestParam(value = "pageNo", required = false) Integer pageNo, @RequestParam(value = "pageSize", required = false) Integer pageSize) {
        return sysTableService.selectColumnPageCondition(mainId, pageNo, pageSize);
    }


    @ApiOperation(value = "admin login")
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public Object adminLogin(@RequestBody JSONObject para) {

        String userInfo = "{\"id\":\"a75d45a015c44384a04449ee80dc3503\",\"username\":\"彭志红\",\"realname\":\"彭志红\",\"avatar\":\"user/20190220/e1fe9925bc315c60addea1b98eb1cb1349547719_1550656892940.jpg\",\"birthday\":\"1\",\"sex\":2,\"email\":\"33333@qq.com\",\"phone\":\"18611111111\",\"orgCode\":\"A01A04\",\"status\":1,\"delFlag\":\"0\",\"workNo\":\"A002\",\"post\":\"002\",\"telephone\":\"0\",\"createBy\":\"admin\",\"createTime\":\"2019-02-13 16:02:36\",\"updateBy\":\"admin\",\"updateTime\":\"2019-11-27 17:41:34\",\"activitiSync\":\"1\",\"identity\":\"1\",\"departIds\":\"1\"}";
        String depts = "[{\"id\":\"67fc001af12a4f9b8458005d3f19934a\",\"parentId\":\"c6d7cb4deeac411cb3384b1b31278596\",\"departName\":\"财务部\",\"departNameEn\":\"1\",\"departNameAbbr\":\"1\",\"departOrder\":0,\"description\":\"1\",\"orgCategory\":\"1\",\"orgType\":\"2\",\"orgCode\":\"A01A04\",\"mobile\":\"1\",\"fax\":\"1\",\"address\":\"1\",\"memo\":\"1\",\"status\":\"1\",\"delFlag\":\"0\",\"createBy\":\"admin\",\"createTime\":\"2019-02-21 16:14:35\",\"updateBy\":\"admin\",\"updateTime\":\"2019-02-25 12:49:41\"}]";
        String result = "{\"multi_depart\":1,\"token\":\"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJleHAiOjE1ODA4OTE2NjAsInVzZXJuYW1lIjoiamVlY2cifQ.PT4IdU9q9SGJtZCPfGRc9xhSKTbqNvVBJXkFJTIKCE8\"}";

        JSONArray jsonDepts = JSONArray.fromObject(depts);
        JSONObject jsonObject = JSONObject.fromObject(userInfo);
        JSONObject jsonResult = JSONObject.fromObject(result);
        jsonResult.put("userInfo", jsonObject);
        jsonResult.put("depts", jsonDepts);
        jsonResult.put("timestamp", "1580889860814");

        return jsonResult;
    }


    @ApiOperation(value = "admin login")
    @RequestMapping(value = "/login/userInfo", method = RequestMethod.GET)
    public Object adminLoginInfo() {
        String userInfo = "{\"totalVisitCount\": 231975, \"todayVisitCount\": 1145, \"todayIp\": 571}";
        JSONObject jsonObject = JSONObject.fromObject(userInfo);
        return jsonObject;
    }

    @ApiOperation(value = "查询table")
    @RequestMapping(value = "/menu/list", method = RequestMethod.GET)
    public Object selectUserMenu(@RequestParam(value = "token", required = false) String token, @RequestParam(value = "_t", required = false) Long timestamp) throws Exception {

        Map<String, Object> result = new HashMap<String, Object>();
        String allAuth = "[{action: \"online:goGenerateCode\", describe: \"代码生成按钮\", type: \"1\", status: \"1\"},\n" +
                "{action: \"online:goGenerateCode\", describe: \"代码生成按钮\", type: \"1\", status: \"1\"},\n" +
                "{action: \"online:sql\", describe: \"SQL增强\", type: \"1\", status: \"1\"},\n" +
                "{action: \"online:ceshi_ruzhi:gwei\", describe: \"岗位字段控制\", type: \"1\", status: \"1\"},\n" +
                "{action: \"online:ceshi_ruzhi:ruz_date\", describe: \"入职时间\", type: \"2\", status: \"1\"},\n" +
                "{action: \"online:ceshi_ruzhi:add\", describe: \"列表添加按钮\", type: \"1\", status: \"1\"},\n" +
                "{action: \"online:ceshi_ruzhi:export\", describe: \"列表导出按钮\", type: \"1\", status: \"1\"},\n" +
                "{action: \"\", describe: \"入职加载数据请求地址\", type: \"1\", status: \"1\"},\n" +
                "{action: \"ruzhi:add\", describe: \"入职添加按钮\", type: \"1\", status: \"1\"},\n" +
                "{action: \"ruzhi:add:back\", describe: \"java添加请求注解\", type: \"1\", status: \"1\"},\n" +
                "{action: \"ruzhi:form:name\", describe: \"表单的入职人员字段控制\", type: \"1\", status: \"1\"},\n" +
                "{action: \"ruzhi:form:sex\", describe: \"入职表单性别\", type: \"2\", status: \"1\"},\n" +
                "{action: \"user:form:phone\", describe: \"手机号禁用\", type: \"2\", status: \"1\"},\n" +
                "{action: \"online:air_china_persion_config:post\", describe: \"用户岗位禁用\", type: \"2\", status: \"1\"},\n" +
                "{action: \"sys:user:list\", describe: \"用户列表数据\", type: \"1\", status: \"0\"},\n" +
                "{action: \"\", describe: \"个人标准积分配置权限\", type: \"0\", status: \"1\"},\n" +
                "{action: \"sysMessageTemplate:delete\", describe: \"删除权限\", type: \"1\", status: \"1\"},\n" +
                "{action: \"user:add\", describe: \"添加用户\", type: \"1\", status: \"1\"},\n" +
                "{action: \"online:air_china_persion_config:account\", describe: \"用户账号显示\", type: \"1\", status: \"1\"},\n" +
                "{action: \"user:edit\", describe: \"编辑用户\", type: \"1\", status: \"1\"},\n" +
                "{action: \"user:syncbpm\", describe: \"同步流程\", type: \"1\", status: \"1\"},\n" +
                "{action: \"user:import\", describe: \"导入请求\", type: \"1\", status: \"1\"}]";

        String auth = "[{action:\"online:ceshi_ruzhi:gwei\", describe: \"岗位字段控制\", type:\"1\"},\n" +
                "{action:\"online:ceshi_ruzhi:gwei\", describe:\"岗位字段控制\", type:\"1\"},\n" +
                "{action:\"online:ceshi_ruzhi:ruz_date\", describe:\"入职时间\", type:\"2\"},\n" +
                "{action:\"online:ceshi_ruzhi:add\", describe:\"列表添加按钮\", type:\"1\"},\n" +
                "{action:\"online:ceshi_ruzhi:export\", describe:\"列表导出按钮\", type:\"1\"},\n" +
                "{action: \"\", describe:\"入职加载数据请求地址\", type:\"1\"}]";


        JSONArray jsonAuth = JSONArray.fromObject(auth); // 字符串转成 JSONArray  对象
        JSONArray jsonAllauth = JSONArray.fromObject(allAuth); // 字符串转成 JSONArray  对象


        List<SysMenuItem> menus = (List<SysMenuItem>) sysMenuService.selectMenuItemList(token);

        List<MenuDto> menuDtoList = new ArrayList<MenuDto>();

        for (SysMenuItem item : menus) {
            if (item.getParent() == null) {
                MenuDto menuDto = new MenuDto();
                MenuMetaDto metaDto = new MenuMetaDto();
                BeanUtils.copyProperties(metaDto, item);
                BeanUtils.copyProperties(menuDto, item);

                menuDto.setMeta(metaDto);
                menuDtoList.add(menuDto);
            } else {
                break;
            }
        }
        String id = "";
        for (MenuDto menuDto : menuDtoList) {
            id = menuDto.getId();
            List<MenuDto> tmpList = new ArrayList<MenuDto>();
            for (SysMenuItem item : menus) {
                if (id.equals(item.getParent())) {

                    MenuDto tmpMenu = new MenuDto();
                    MenuMetaDto metaDto = new MenuMetaDto();
                    BeanUtils.copyProperties(metaDto, item);
                    BeanUtils.copyProperties(tmpMenu, item);
                    tmpMenu.setMeta(metaDto);
                    tmpList.add(tmpMenu);

                }
                if (!tmpList.isEmpty()) {
                    MenuDto[] menuArray = tmpList.toArray(new MenuDto[tmpList.size()]);
                    menuDto.setChildren(menuArray);
                }
            }
        }


        result.put("allAuth", jsonAllauth);
        result.put("auth", jsonAuth);
        result.put("menu", menuDtoList);



      return result;
    }


}
